package com.film.admincontroller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.film.domain.Performer;
import com.film.service.PerformerService;
import com.film.service.impl.PerformerServiceImpl;

@WebServlet("/adminAddPerformer.do")
public class AdminAddPerformerController extends HttpServlet{
	private PerformerService performerService = new PerformerServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {		
		
			req.setCharacterEncoding("utf-8");
	//		
			//文件上传
			//创建factory工厂
			DiskFileItemFactory factory = new DiskFileItemFactory();
			
			//servletFileUpload对象
			ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
			servletFileUpload.setHeaderEncoding("UTF-8");
			List<String> list = new ArrayList<>();
			
			try {
	
				List<FileItem> items = servletFileUpload.parseRequest(req);
	            //遍历
				for(FileItem item:items){
					//首先判断是普通文本项还是文件项，文件项与普通项由不同的API来处理
					if(item.isFormField()){
						//true表示普通文本项
						//获取文本项的name属性值
						//是普通的组件
						String value = item.getString("UTF-8");
						
						list.add(value);
					}else{
						//false表示文件项
						//获取文件名称
						String name=item.getName();
						
						Long time = new Date().getTime();
						
						String realName ="img/performer_img/"+time+""+name;
						String filmname = time+""+name;
						list.add(realName);
						//获取文件项的输入流
						InputStream in=item.getInputStream();
						//获取服务器端文件存储的目标磁盘路径
						String path=getServletContext().getRealPath("img/performer_img");
						
						//获取输出流，输出到本地文件中
						File file = new File(path);
						if(!file.exists()) {
							file.mkdir();
							
						}
						File files = new File(path+"/"+filmname);
						OutputStream out=new FileOutputStream(path+"/"+filmname);
						
	////					//写入数据
						int len=0;
						byte[] b=new byte[1024];                    
						while((len=in.read(b))!=-1){
							out.write(b,0,len);
						}
						in.close();
						out.close();
					}
				}
				System.out.println(list);
				
			} catch (FileUploadException e) {
				System.out.println("出错啦");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Performer performer = new Performer(list.get(0),list.get(1),list.get(2),list.get(4),list.get(5),list.get(6),list.get(7),list.get(8),list.get(9),list.get(10),list.get(3));
			System.out.println(performer);
			int result = performerService.addPerformer(performer);
			if(result ==1) {
				resp.sendRedirect("/film/adminPerformer.do");
			}
			
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
}
